<?php
/**
 * Created by PhpStorm.
 * User: sunlight
 * Date: 2018/11/27
 * Time: 17:24
 */

namespace app\api\controller\V1;

use app\api\controller\Common;
use app\common\lib\exception\ApiException;

class Store extends Common{

    /**
     * 门店列表
     * @return \think\response\Json
     */
    public function index(){
        $data=input('get.');
        $whereData['status'] = config('code.status_normal');
        if (!empty($data['catid'])){
            $whereData['catid'] = input('get.catid',0,'intval');
        }

        if (!empty($data['address'])){
            $whereData['address'] = input('get.address',0,'intval');
        }

        if (!empty($data['title'])){
            $whereData['title']=['like','%'.$data['title'].'%'];
        }

        $this->getPageAndSize($data);
        $total=model('Store')->getProductCountByCondition($whereData);
        $news=model('Store')->getProductByCondition($whereData,$this->from,$this->size);

        $result=[
            'total' => $total,
            'page_num' => ceil($total / $this -> size),
            'list' => $this -> getDealProduct($news)
        ];

        return show(config('code.success'),'ok',$result,200);
    }

    /**
     * 门店详情
     * @return \think\response\Json
     */
    public function read(){
        $id=input('param.id',0,'intval');
        if (empty($id)){
            return new ApiException('id is not',404);
        }

        //通过id去获取数据表里面的数据  try catch untodao
        $news=model('Store')->get($id);
        if (empty($news) || $news->status != config('code.status_normal')){
            return new ApiException('不存在该文章',404);
        }

        try{
            model('Store')->where(['id'=>$id])->setInc('read_count');
        }catch (Exception $e){
            return new ApiException('error',400);
        }

        //获取栏目
        $cats=config('cat.lists');
        $news->catname=$cats[$news->catid];

        //获取区域
        $adres=config('address.addresslists');
        $news->addressname=$adres[$news->address];

        return show(config('code.success'), 'OK', $news, 200);
    }

}